package com.cn.wanxi.service.impl;

import com.cn.wanxi.dao.INewsDao;
import com.cn.wanxi.dao.impl.NewsDaoImpl;
import com.cn.wanxi.model.NewsModel;
import com.cn.wanxi.model.OrdbyModel;
import com.cn.wanxi.service.INewsService;
import redis.clients.jedis.Jedis;

import java.util.ArrayList;
import java.util.List;

public class NewsServiceImpl implements INewsService {


    @Override
    public int deleteById(int newsId) {
        INewsDao iNewsDao=new NewsDaoImpl();
        int count=iNewsDao.deleteById(newsId);
        if (count == 1){
            Jedis jedis=new Jedis();
            jedis.del("id");
            jedis.del("title");
            jedis.del("date");
            jedis.del("author");
            jedis.del("content");
            jedis.del("newstype");
            jedis.del("detailed");
            jedis.del("number");
        }
        return count;
    }

    @Override
    public NewsModel findById(int newsid) {
        INewsDao iNewsDao=new NewsDaoImpl();
        int count=iNewsDao.updateNumber(newsid);
        if (count==1){
            Jedis jedis=new Jedis();
            jedis.del("id");
            jedis.del("title");
            jedis.del("date");
            jedis.del("author");
            jedis.del("content");
            jedis.del("newstype");
            jedis.del("detailed");
            jedis.del("number");
        }
        NewsModel newsModel=iNewsDao.findById(newsid);
        return newsModel;
    }

    @Override
    public int findUpdate(NewsModel newsModel) {
        INewsDao iNewsDao=new NewsDaoImpl();
        int count=iNewsDao.newsUpdate(newsModel);
        if (count==1){
            Jedis jedis=new Jedis();
            jedis.del("id");
            jedis.del("title");
            jedis.del("date");
            jedis.del("author");
            jedis.del("content");
            jedis.del("newstype");
            jedis.del("detailed");
            jedis.del("number");
        }
        return count;
    }

    @Override
    public int newsAdd(NewsModel newsModel) {
        INewsDao iNewsDao=new NewsDaoImpl();
        int count=iNewsDao.newsAdd(newsModel);
        if (count==1){
            Jedis jedis=new Jedis();
            jedis.del("id");
            jedis.del("title");
            jedis.del("date");
            jedis.del("author");
            jedis.del("content");
            jedis.del("newstype");
            jedis.del("detailed");
            jedis.del("number");
        }
        return count;
    }


    @Override
    public List<NewsModel> newsFindAllTh(NewsModel newsModel) {
        INewsDao iNewsDao=new NewsDaoImpl();
        List<NewsModel> list=new ArrayList<>();
        Jedis jedis=new Jedis();
        Long length= jedis.llen("id");
        //判断模糊查询和分页的model是否为空，不为空就直接访问数据库，如果为空判断redis是否有数据
        if (!newsModel.getOrdbyModel().getOrdbydate().equals("") || !newsModel.getTitle().equals("") || !newsModel.getStartdate().equals("") || !newsModel.getEnddate().equals("")
                || !newsModel.getAuthor().equals("")  || !newsModel.getContent().equals("") || !newsModel.getNewstype().equals("")
                || newsModel.getPageModel().getPageSize()!=10 || newsModel.getPageModel().getPageNum()!=1){
            list=iNewsDao.newsFindAllTh(newsModel);
        }else if (length>0){
            List<String> id=jedis.lrange("id",0,length);
            List<String> title=jedis.lrange("title",0,length);
            List<String> date=jedis.lrange("date",0,length);
            List<String> author=jedis.lrange("author",0,length);
            List<String> content=jedis.lrange("content",0,length);
            List<String> newstype=jedis.lrange("newstype",0,length);
            List<String> detailed=jedis.lrange("detailed",0,length);
            List<String> number=jedis.lrange("number",0,length);
            for (int i=0;i<length;i++){
                NewsModel model = new NewsModel();
                model.setId(Integer.parseInt(id.get(i)));
                model.setTitle(title.get(i));
                model.setDate(date.get(i));
                model.setAuthor(author.get(i));
                model.setContent(content.get(i));
                model.setDetailed(detailed.get(i));
                model.setNewstype(newstype.get(i));
                model.setNumber(number.get(i));
                list.add(model);
            }
        }else {
            list=iNewsDao.newsFindAllTh(newsModel);
            for ( NewsModel model:list) {
                jedis.rpush("id",model.getId().toString());
                jedis.rpush("title",model.getTitle());
                jedis.rpush("date",model.getDate());
                jedis.rpush("author",model.getAuthor());
                jedis.rpush("content",model.getContent());
                jedis.rpush("newstype",model.getNewstype());
                jedis.rpush("detailed",model.getDetailed());
                jedis.rpush("number",model.getNumber());
            }
        }
        return list;
    }


    @Override
    public List<NewsModel> newsFindAllsom() {
        INewsDao iNewsDao=new NewsDaoImpl();
        List<NewsModel> list=new ArrayList<>();
        Jedis jedis=new Jedis();
        Long length= jedis.llen("id");
        //判断模糊查询和分页的model是否为空，不为空就直接访问数据库，如果为空判断redis是否有数据
        if (length>0){
            List<String> id=jedis.lrange("id",0,length);
            List<String> title=jedis.lrange("title",0,length);
            List<String> date=jedis.lrange("date",0,length);
            List<String> author=jedis.lrange("author",0,length);
            List<String> content=jedis.lrange("content",0,length);
            List<String> newsdetailed=jedis.lrange("detailed",0,length);
            List<String> newstype=jedis.lrange("newstype",0,length);
            List<String> number=jedis.lrange("number",0,length);
            for (int i=0;i<length;i++){
                NewsModel model = new NewsModel();
                model.setId(Integer.parseInt(id.get(i)));
                model.setTitle(title.get(i));
                model.setDate(date.get(i));
                model.setAuthor(author.get(i));
                model.setContent(content.get(i));
                model.setDetailed(newsdetailed.get(i));
                model.setNewstype(newstype.get(i));
                model.setNumber(number.get(i));
                list.add(model);
            }
        }else {
            list=iNewsDao.newsFindAllsom();
            for ( NewsModel model:list) {
                jedis.rpush("id",model.getId().toString());
                jedis.rpush("title",model.getTitle());
                jedis.rpush("date",model.getDate());
                jedis.rpush("author",model.getAuthor());
                jedis.rpush("content",model.getContent());
                jedis.rpush("detailed",model.getDetailed());
                jedis.rpush("newstype",model.getNewstype());
                jedis.rpush("number",model.getNumber());
            }
        }
        return list;
    }




    @Override
    public int getCont(NewsModel newsModel) {
        INewsDao iNewsDao=new NewsDaoImpl();
        return iNewsDao.getCount(newsModel);
    }

    @Override
    public List<NewsModel> NewsTypeFindAll() {
        INewsDao iNewsDao=new NewsDaoImpl();
        List<NewsModel> list=iNewsDao.NewsTypeFindAll();
        return list;
    }

    @Override
    public List<NewsModel> newsNimitFind(NewsModel newsModel) {
        INewsDao iNewsDao=new NewsDaoImpl();
        List<NewsModel> list=iNewsDao.newsNimitFind(newsModel);
        return list;
    }

    @Override
    public List<NewsModel> newstypeFind(NewsModel newsModel) {
        INewsDao iNewsDao=new NewsDaoImpl();
        List<NewsModel> list=iNewsDao.newstypeFind(newsModel);
        return list;
    }

    @Override
    public int getTypeCont(NewsModel newsModel) {
        INewsDao iNewsDao=new NewsDaoImpl();
        return iNewsDao.getTypeCont(newsModel);
    }

}
